package com.example.project.service.impl;

import com.example.project.dao.TcdmSecureHazardAssessmentRecycleDao;
import com.example.project.dao.TcdmSecureHazardListDao;
import com.example.project.entity.TcdmSecureHazardAssessment;
import com.example.project.dao.TcdmSecureHazardAssessmentDao;
import com.example.project.entity.TcdmSecureHazardAssessmentRecycle;
import com.example.project.entity.TcdmSecureHazardList;
import com.example.project.service.TcdmSecureHazardAssessmentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (TcdmSecureHazardAssessment)表服务实现类
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@Service("tcdmSecureHazardAssessmentService")
public class TcdmSecureHazardAssessmentServiceImpl implements TcdmSecureHazardAssessmentService {
    @Resource
    private TcdmSecureHazardAssessmentDao tcdmSecureHazardAssessmentDao;

    @Resource
    private TcdmSecureHazardAssessmentRecycleDao tcdmSecureHazardAssessmentRecycleDao;

    @Resource
    private TcdmSecureHazardListDao tcdmSecureHazardListDao;
    /**
     * 通过ID查询单条数据
     *
     * @param tshaId 主键
     * @return 实例对象
     */
    @Override
    public TcdmSecureHazardAssessment queryById(Integer tshaId) {
        return this.tcdmSecureHazardAssessmentDao.queryById(tshaId);
    }

    @Override
    public List<TcdmSecureHazardAssessment> findList() {
        return this.tcdmSecureHazardAssessmentDao.findList();
    }

    /**
     * 分页查询
     *
     * @param tcdmSecureHazardAssessment 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TcdmSecureHazardAssessment> queryByPage(TcdmSecureHazardAssessment tcdmSecureHazardAssessment, PageRequest pageRequest,Date star,Date end) {
        long total = this.tcdmSecureHazardAssessmentDao.count(tcdmSecureHazardAssessment);
        return new PageImpl<>(this.tcdmSecureHazardAssessmentDao.queryAllByLimit(tcdmSecureHazardAssessment, pageRequest,star,end), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tcdmSecureHazardAssessment 实例对象
     * @return 实例对象
     */
    @Override
    public TcdmSecureHazardAssessment insert(TcdmSecureHazardAssessment tcdmSecureHazardAssessment) {
        tcdmSecureHazardAssessment.setTshaStat(new Date());
        this.tcdmSecureHazardAssessmentDao.insert(tcdmSecureHazardAssessment);
        return tcdmSecureHazardAssessment;
    }

    /**
     * 修改数据
     *
     * @param tcdmSecureHazardAssessment 实例对象
     * @return 实例对象
     */
    @Override
    public TcdmSecureHazardAssessment update(TcdmSecureHazardAssessment tcdmSecureHazardAssessment) {
        this.tcdmSecureHazardAssessmentDao.update(tcdmSecureHazardAssessment);
        return this.queryById(tcdmSecureHazardAssessment.getTshaId());
    }

    /**
     * 通过主键删除数据
     *
     * @param tshaId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer tshaId) {
        return this.tcdmSecureHazardAssessmentDao.deleteById(tshaId) > 0;
    }

    @Override
    public boolean deleteById1(Integer tshaId) {
        TcdmSecureHazardAssessment tcdmSecureHazardAssessment=this.tcdmSecureHazardAssessmentDao.queryById(tshaId);
        if (tcdmSecureHazardAssessment!=null){
            tcdmSecureHazardAssessment.setValid(0);

            TcdmSecureHazardAssessmentRecycle tcdmSecureHazardAssessmentRecycle=new TcdmSecureHazardAssessmentRecycle();
            tcdmSecureHazardAssessmentRecycle.setTshaId(tshaId);

            tcdmSecureHazardAssessmentRecycle.setTshareUt(new Date());
            tcdmSecureHazardAssessmentRecycle.setTshaName(tcdmSecureHazardAssessment.getTshaName());
            tcdmSecureHazardAssessmentRecycle.setTshaNumber(tcdmSecureHazardAssessment.getTshaNumber());

            this.tcdmSecureHazardAssessmentRecycleDao.insert(tcdmSecureHazardAssessmentRecycle);

            return this.tcdmSecureHazardAssessmentDao.update(tcdmSecureHazardAssessment)>0;
        }
        else return false;

    }

    @Override
    public boolean deleteByValid() {
        return this.tcdmSecureHazardAssessmentDao.deleteByValid();
    }

//    @Override
//    public Page<TcdmSecureHazardList> findlist(TcdmSecureHazardAssessment tcdmSecureHazardAssessment, PageRequest pageRequest) {
//        TcdmSecureHazardList tcdmSecureHazardList=new TcdmSecureHazardList();
//        tcdmSecureHazardList.setTshlUnit(tcdmSecureHazardAssessment.getTshaName());
//        return this.tcdmSecureHazardListDao.queryAllByLimit();
//    }

//    @Override
//    public Page<TcdmSecureHazardList> findlist(TcdmSecureHazardAssessment tcdmSecureHazardAssessment) {
//        //根据责任单位查询出对应清单
//        return this.tcdmSecureHazardListDao.findByUnit(tcdmSecureHazardAssessment.getTshaName());
//    }
}
